package com.lanou3g.hw0518.model;

import com.lanou3g.hw0518.pojo.User;
import com.lanou3g.hw0518.util.JDBCUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class RegisterModel {
    public boolean register(User user){
        Connection conn=JDBCUtil.getConn();
        try {
            //开启事务
//            conn.setAutoCommit(false);
            String sqlInsert = "insert into user (username,pwd,nickname"+",gender) values (?,?,?,?)";
            PreparedStatement psInsert = conn.prepareStatement(sqlInsert);
            psInsert.setString(1,user.getUsername());
            psInsert.setString(2,user.getPassword());
            psInsert.setString(3,user.getNickName());
            psInsert.setString(4,user.getGender());
            psInsert.executeUpdate();
            //查询刚刚插入的主键
            String getIDSQL = "SELECT MAX(uid) FROM user";
            PreparedStatement psID= conn.prepareStatement(getIDSQL);
            ResultSet re= psID.executeQuery();
            re.next();
            int uid=re.getInt(1);
            re.close();

            psID.close();

            //查询爱好的id 然后插入到user_hobby表中
            String hobbySQL = "SELECT hid FROM hobby where opt =? ";
            String hobbyInsertSQL="INSERT INTO user_hobby (hid,uid) values (?,?)";
            PreparedStatement hobbyPs = conn.prepareStatement(hobbySQL);
            PreparedStatement hobbyInsertPs = conn.prepareStatement(hobbyInsertSQL);
            for (String hobby : user.getHobbies()) {
                //根据名字查询出hobby的信息
                hobbyPs.setString(1,hobby);
                ResultSet hobbyRs = hobbyPs.executeQuery();

                hobbyRs.next();
                int hid = hobbyRs.getInt(1);
                hobbyInsertPs.setInt(1,hid);
                hobbyInsertPs.setInt(2,uid);
                hobbyInsertPs.executeUpdate();
                hobbyRs.close();



            }
            hobbyInsertPs.close();
            hobbyPs.close();

            //提交事务

//            conn.commit();

            return true;//注册成功
        }catch (Exception e){
            try {
                //在catch中回滚
                //如果哪条sql抛异常.我们可以回滚到
                //sql执行之前
                conn.rollback();
            } catch (Exception e1) {
                e1.printStackTrace();
            }

        }
        return false;
    }
    public boolean  hasThisName(String username){
        String sql = "select count(*) from user "+" where username = ?";
        Connection conn = JDBCUtil.getConn();
        try {
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1,username);
            ResultSet resultSet = ps.executeQuery();
            resultSet.next();
            int count = resultSet.getInt(1);
            resultSet.close();
            ps.close();

            return  count>0;

        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
}
